package com.mm.util.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述: 生成Cron表达式
 *
 * @author: yanglin
 * @Date: 2021-07-27-10:38
 * @Version: 1.0
 */
public class CronUtil {

    /**
     * 生成Cron表达式
     *
     * @param type   0:每天一次; 1:每周一次; 2:每月一次; 3:具体时间(执行一次)
     * @param params
     * @return
     */
    public static String createCron(Integer type, Integer... params) {
        // type 0:每天一次; 1:每周一次; 2:每月一次; 3:具体时间(执行一次)
        String cron = "";
        // 秒 0-59
        Integer second = params[0] == null ? 1 : params[0];
        // 分 0-59
        Integer minute = params[1] == null ? 0 : params[1];
        // 时 0-23
        Integer hour = params[2] == null ? 0 : params[2];
        // 天 1-31
        Integer day;
        // 周 1-7
        Integer week;
        // 月 1-12
        Integer month;
        switch (type) {
            // 每天的hour时;minute分执行;second秒执行
            case 0:
                cron = second + " " + minute + " " + hour + " * * ? ";
                break;
            // 每周的week的hour时;minute分执行;second秒执行
            case 1:
                // 周 1-7
                week = params[3] == null ? 1 : params[3];
                cron = second + " " + minute + " " + hour + " ? * " + week + " *";
                break;
            // 每月的day号的hour时;minute分执行;second秒执行
            case 2:
                day = params[3] == null ? 1 : params[3];
                cron = second + " " + minute + " " + hour + " " + day + " * ? *";
                break;
            // 具体时间 year年;month月;day天;hour时;minute分执行;second秒执行
            case 3:
                day = params[3] == null ? 1 : params[3];
                // 月 1-12
                month = params[4] == null ? 1 : params[4];
                cron = second + " " + minute + " " + hour + " " + day + " " + month + " ? *";
                break;
        }
        return cron;
    }

    /**
     * 生成Cron表达式
     *
     * @param type   0:每天一次; 1:每周一次; 2:每月一次; 3:具体时间(执行一次)
     * @param params
     * @return
     */
    public static String createCronStr(Integer type, Integer... params) {
        // type 0:每天一次; 1:每周一次; 2:每月一次; 3:具体时间(执行一次)
        String cron = "";
        // 秒 0-59
        Integer second = params[0] == null ? 1 : params[0];
        // 分 0-59
        Integer minute = params[1] == null ? 0 : params[1];
        // 时 0-23
        Integer hour = params[2] == null ? 0 : params[2];
        // 天 1-31
        Integer day;
        // 周 1-7
        Integer week;
        // 月 1-12
        Integer month;
        switch (type) {
            // 每天的hour时;minute分执行;second秒执行
            case 0:
                cron = "每天的" + hour + "时" + minute + "分" + second + "秒执行";
                break;
            // 每周的week的hour时;minute分执行;second秒执行
            case 1:
                // 周 1-7
                week = params[3] == null ? 1 : params[3];
                cron = "每周的周" + week + ";" + hour + "时" + minute + "分" + second + "秒执行";
                break;
            // 每月的day号的hour时;minute分执行;second秒执行
            case 2:
                day = params[3] == null ? 1 : params[3];
                cron = "每月的" + day + "号" + hour + "时" + minute + "分" + second + "秒执行";
                break;
            // 具体时间 今年;month月;day天;hour时;minute分执行;second秒执行
            case 3:
                day = params[3] == null ? 1 : params[3];
                // 月 1-12
                month = params[4] == null ? 1 : params[4];
                cron = month + "月" + day + "号" + hour + "时" + minute + "分" + second + "秒执行";
                break;
        }
        return cron;
    }

    /**
     * 通过输入指定日期时间生成cron表达式
     *
     * @param date
     * @return cron表达式
     */
    public static String createCron(Date date) {
        String dateFormat = "ss mm HH dd MM ? *";
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }

        return formatTimeStr;
    }


    public static void main(String[] args) {

        System.out.println(CronUtil.createCronStr(0, 0, 12, 11));
        System.out.println(CronUtil.createCron(0, 0, 12, 11));

        System.out.println(CronUtil.createCronStr(1, 0, 12, 11, 7));
        System.out.println(CronUtil.createCron(1, 0, 12, 11, 7));

        System.out.println(CronUtil.createCronStr(2, 0, 12, 11, 28));
        System.out.println(CronUtil.createCron(2, 0, 12, 11, 28));

        System.out.println(CronUtil.createCronStr(3, 0, 12, 11, 28, 7));
        System.out.println(CronUtil.createCron(3, 0, 12, 11, 28, 7));

        System.out.println(CronUtil.createCron(new Date()));

        System.out.println(CronUtil.createCronStr(0, 0, 5, 0));
        System.out.println(CronUtil.createCron(0, 0, 5, 0));

    }
}
